<?php
/**
 * Converts PDOStatement to PHP data.
 */
class SQLResults {
	/**
	 * Fetches first value of first row from ResultSet.
	 * 
	 * @param PDOStatement $stmt
	 * @return string|number
	 */
	public static function toValue($stmt) {
		return $stmt->fetchColumn();
	}

	/**
	 * Fetches row from ResultSet.
	 *
	 * @param PDOStatement $stmt
	 * @return array
	 */
	public static function toRow($stmt) {
		return $stmt->fetch(PDO::FETCH_ASSOC);
	}
	
	/**
	 * Fecthes first column of all rows from ResultSet.
	 * 
	 * @param PDOStatement $stmt
	 * @return array
	 */
	public static function toColumn($stmt) {
		return $stmt->fetchAll(PDO::FETCH_COLUMN,0);
	}
	
	/**
	 * Fetches all rows from Resultset and makes one column key and other value.
	 * 
	 * @param PDOStatement $stmt
	 * @param string $strColumnKeyName
	 * @param string $strColumnValueName
	 * @return array
	 */
	public static function toMap($stmt, $strColumnKeyName, $strColumnValueName) {
		$tblTMP = $stmt->fetchAll(PDO::FETCH_ASSOC);
		if(!sizeof($tblTMP)) return array();
		$tblOutput=array();
		foreach($tblTMP as $tblRow) {
			$tblOutput[$tblRow[$strKey]]=$tblRow[$strValue];
		}
		return $tblOutput;
	}
	
	/**
	 * Fetches all rows from Resultset.
	 * 
	 * @param PDOStatement $stmt
	 * @return array
	 */
	public static function toList($stmt) {
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
}